Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #define POT_PIN 0 // Analog input pin for POT
- #define ENABLE_PIN 2 // GPIO input pin for enable/disable
- #define PWM1_PIN 1 // PWM output pin 1
- #define PWM2_PIN 3 // PWM output pin 2
- int potValue = 0;
- int softStartValue = 0;
- int pwm1Value = 0;
- int pwm2Value = 0;
- bool isEnabled = false;
- unsigned long softStartTime = 0;
- void setup() {
- pinMode(ENABLE_PIN, INPUT_PULLUP);
- pinMode(PWM1_PIN, OUTPUT);
- pinMode(PWM2_PIN, OUTPUT);
- analogReference(DEFAULT);
- }
- void softStart() {
- unsigned long currentTime = millis();
- if (currentTime - softStartTime < 1000) {
- int softStartPercentage = map(currentTime - softStartTime, 0, 1000, 0, 100);
- pwm1Value = map(softStartPercentage, 0, 100, 20, softStartValue);
- } else {
- pwm1Value = softStartValue;
- }
- }
- void loop() {
- isEnabled = digitalRead(ENABLE_PIN);
- if (isEnabled) {
- potValue = analogRead(POT_PIN);
- softStartValue = map(potValue, 0, 1023, 0, 100);
- softStart();
- if (potValue <= 511) {
- pwm2Value = 0;
- digitalWrite(PWM2_PIN, LOW);
- } else {
- pwm2Value = map(potValue, 512, 1023, 0, 100);
- digitalWrite(PWM2_PIN, HIGH);
- }
- analogWrite(PWM1_PIN, map(pwm1Value, 0, 100, 51, 255)); // Map to 51-255 (20%-100%)
- analogWrite(PWM2_PIN, map(pwm2Value, 0, 100, 51, 255)); // Map to 51-255 (20%-100%)
- } else {
- digitalWrite(PWM1_PIN, LOW);
- digitalWrite(PWM2_PIN, LOW);
- }
- }
Add Comment
Please, Sign In to add comment